package org.telluriumsource.ui.widget.dojo.object;

import demo.tellurium.framework.BaseDojoWidget 

import org.json.simple.JSONObject;

/**
 * Tellurium Widget object for the Dojo TooltipDialogTextBox widget
 *
 * @author Roger Xu (roger.xu.2007@gmail.com)
 *
 * Date: Apr 14, 2010
 * 
 */
class AutoCompleteTextBox extends BaseDojoWidget {

    private static final String WIDGET_NAME = AutoCompleteTextBox.class.getSimpleName();

    private String widgetId = null;

    @Override
    public String getWidgetName() {
        return WIDGET_NAME;
    }

    @Override
    public JSONObject toJSON() {
        return buildJSON() {jso ->
            jso.put(UI_TYPE, NAMESPACE + "_" + getWidgetName());
        }
    }

    @Override
    public void defineWidget() {
        ui.Container(uid: "${WIDGET_NAME}") {
            Container(uid: "TextBox", clocator: [tag: "div", class: "dijitTextBox", widgetid: "${widgetId}"]) {
                Container(uid: "InputField", clocator: [tag: "div", class: "dijitInputField"]) {
                    InputBox(uid: "InputTextBox", clocator: [tag: "input", type: "text"], respond: ["keyPress"])
                }
            }
            Container(uid: "PopUpList") {
                List(uid: "Options", clocator: [tag: "ul", id: "${widgetId}_popup"], separator: "li:visible") {
                    UrlLink(uid: "{all}", respond: ["mouseDown", "mouseUp", "mouseOver"])
                }
            }
        }
    }

    public void inputText(String input) {
        def textbox = "${WIDGET_NAME}.TextBox";
        def inputTextBox = "${textbox}.InputField.InputTextBox";
        clearText inputTextBox
        pause 200
        keyType inputTextBox, input
    }

    public String getValue() {
        def textbox = "${WIDGET_NAME}.TextBox";
        def inputTextBox = "${textbox}.InputField.InputTextBox";
        return getValue(inputTextBox);
    }

    public void selectItem(int index) {
        // click option list
        def listUid = "${WIDGET_NAME}.PopUpList.Options";
        waitForElementPresent listUid, 5000
        if (!isElementPresent(listUid)) {
            console.debug("not show", listUid);
            return;
        }

        clickListOption(listUid, index);
    }
}